const express = require('express');
const router = express.Router();
const { 
  createPost, 
  getPosts, 
  getPost, 
  updatePost, 
  deletePost 
} = require('../controllers/postController');
const { protect } = require('../middleware/auth');

/**
 * @swagger
 * tags:
 *   name: Posts
 *   description: 文章相关接口
 */


// 获取所有文章
/**
 * @swagger
 * /api/posts:
 *   get:
 *     tags: [Posts]
 *     summary: 获取所有文章
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 成功获取所有文章
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 posts:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       _id:
 *                         type: string
 *                       title:
 *                         type: string
 *                       content:
 *                         type: string
 *                       author:
 *                         type: string
 *                       category:
 *                         type: string
 *                 pagination:
 *                   type: object
 *                   properties:
 *                     current:
 *                       type: integer
 *                     total:
 *                       type: integer
 *                     totalRecords:
 *                       type: integer
 */

router.get('/', getPosts);

// 获取单篇文章
router.get('/:id', getPost);

// 创建文章（需要登录）
/**
 * @swagger
 * /api/posts:
 *   post:
 *     tags: [Posts]
 *     summary: 创建文章  
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - title
 *               - content
 *               - category
 *             properties:
 *               title:
 *                 type: string
 *                 example: 文章标题
 *               content:
 *                 type: string
 *                 example: 文章内容
 *               category:
 *                 type: string
 *                 example: 技术
 *               tags:
 *                 type: array
 *                 items:
 *                   type: string
 *                 example: ["JavaScript", "Node.js"]
 *     responses:
 *       201:
 *         description: 文章创建成功
 *       401:
 *         description: 未授权
 */
router.post('/', protect, createPost);

// 更新文章（需要登录）
/**
 * @swagger
 * /api/posts/{id}:
 *   put:
 *     tags: [Posts]
 *     summary: 更新文章
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *                 example: 更新后的文章标题
 *               content:
 *                 type: string
 *                 example: 更新后的文章内容
 *               category:
 *                 type: string
 *                 example: 技术
 *               tags:
 *                 type: array
 *                 items:
 *                   type: string
 *                 example: ["JavaScript", "Node.js"]
 *     responses:
 *       200:
 *         description: 文章更新成功
 *       401:
 *         description: 未授权
 */
router.put('/:id', protect, updatePost);

// 删除文章（需要登录）
/**
 * @swagger
 * /api/posts/{id}:
 *   delete:
 *     tags: [Posts]
 *     summary: 删除文章
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 文章删除成功
 *       401:
 *         description: 未授权
 */

router.delete('/:id', protect, deletePost);

module.exports = router;
